package com.example.designpatterns.command.concrete;

import com.example.designpatterns.command.Command;
import com.example.designpatterns.command.receiver.Door;

/**
 * DoorCloseCommand类 - 门关闭命令
 */
public class DoorCloseCommand implements Command {
    private Door door;
    private boolean wasOpen;
    
    /**
     * 创建门关闭命令
     * @param door 门对象
     */
    public DoorCloseCommand(Door door) {
        this.door = door;
    }
    
    @Override
    public void execute() {
        // 保存当前状态以便撤销
        wasOpen = door.isOpen();
        
        door.close();
    }
    
    @Override
    public boolean undo() {
        if (wasOpen) {
            // 如果门锁定，先解锁
            if (door.isLocked()) {
                door.unlock();
            }
            door.open();
            return true;
        }
        return false;
    }
    
    @Override
    public String getName() {
        return "关闭" + door.getLocation() + "的门";
    }
} 